﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace Software_2Project.DB_Layer
{
    class DB_Shuffle
    {
        public string ConnectionString =
   "Data Source=crespooo;Initial Catalog=ATM;Integrated Security=True;MultipleActiveResultSets=true";
        SqlConnection con = new SqlConnection();

        DataTable dt = new DataTable();
       
        public Int32 dbbalance;

        public bool checkShuffleWithdraw(Int32 acconum, Int32 balance)
        {
            con.ConnectionString = ConnectionString;
            if (ConnectionState.Closed == con.State)
                con.Open();
            SqlCommand cmd1 = new SqlCommand("SELECT balance FROM bank where acconum = " + acconum + " ", con);
            SqlDataReader rd3 = cmd1.ExecuteReader();
            rd3.Read();

            dbbalance = Convert.ToInt32(rd3["balance"]);
            if (balance > dbbalance)
            {
                con.Close();
                return false;
            }
            else
            {
                con.Close();
            return true;
            }

        }
        public DataTable shuffleWithdraw(Int32 acconum, Int32 balance)
        {
            con.ConnectionString = ConnectionString;
            try
            {
                if (ConnectionState.Closed == con.State)
                    con.Open();

                SqlCommand cmd = new SqlCommand("SELECT balance FROM bank where acconum = " + acconum + " ", con);
                SqlDataReader rd = cmd.ExecuteReader();
                rd.Read();
                dbbalance = Convert.ToInt32(rd["balance"]);



                SqlCommand cmd1 = new SqlCommand("UPDATE bank  SET balance = " + dbbalance + -+balance + " WHERE acconum = " + acconum + " ", con);
                SqlDataReader rd1 = cmd1.ExecuteReader();

                SqlCommand cmmd = new SqlCommand("SELECT acconum,password FROM bank ", con);
                SqlDataReader rd2 = cmmd.ExecuteReader();
                dt.Load(rd2);
                //con.Close();
                return dt;
                

            }

            catch
            {
                con.Close();
                throw;
            }

        }
    }
}
